/**
 * 移动端适配工具函数
 */

// 检测设备类型
export const isMobile = (): boolean => {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
};

// 检测是否是iOS设备
export const isIOS = (): boolean => {
  return /iPad|iPhone|iPod/.test(navigator.userAgent);
};

// 检测是否是Android设备
export const isAndroid = (): boolean => {
  return /Android/.test(navigator.userAgent);
};

// 获取设备屏幕宽度
export const getScreenWidth = (): number => {
  return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
};

// 获取设备屏幕高度
export const getScreenHeight = (): number => {
  return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
};

// 根据屏幕宽度判断当前设备类型
export const getDeviceType = (): 'mobile' | 'tablet' | 'desktop' => {
  const width = getScreenWidth();
  if (width < 768) {
    return 'mobile';
  } else if (width < 1024) {
    return 'tablet';
  } else {
    return 'desktop';
  }
};

// 监听屏幕方向变化
export const listenOrientationChange = (callback: (isLandscape: boolean) => void): (() => void) => {
  const handler = () => {
    const isLandscape = window.innerWidth > window.innerHeight;
    callback(isLandscape);
  };
  
  window.addEventListener('resize', handler);
  handler(); // 初始调用一次
  
  // 返回取消监听的函数
  return () => {
    window.removeEventListener('resize', handler);
  };
};

// 禁用页面缩放（在某些场景可能需要）
export const disableZoom = (): void => {
  document.addEventListener('touchstart', (event) => {
    if (event.touches.length > 1) {
      event.preventDefault();
    }
  }, { passive: false });
  
  let lastTouchEnd = 0;
  document.addEventListener('touchend', (event) => {
    const now = Date.now();
    if (now - lastTouchEnd < 300) {
      event.preventDefault();
    }
    lastTouchEnd = now;
  }, { passive: false });
};

// 适配iPhone X 及以上机型底部安全区域
export const setupSafeAreaBottom = (): void => {
  const html = document.documentElement;
  if (isIOS() && /iPhone X|iPhone 11|iPhone 12|iPhone 13|iPhone 14|iPhone 15/i.test(navigator.userAgent)) {
    html.style.setProperty('--safe-area-inset-bottom', 'env(safe-area-inset-bottom)');
  } else {
    html.style.setProperty('--safe-area-inset-bottom', '0');
  }
};

// 移动端适配初始化
const initMobileAdapter = (): void => {
  // 检测设备类型，添加对应的class到html元素
  const html = document.documentElement;
  const deviceType = getDeviceType();
  html.classList.add(deviceType);
  
  // 监听屏幕方向变化
  listenOrientationChange((isLandscape) => {
    if (isLandscape) {
      html.classList.add('landscape');
      html.classList.remove('portrait');
    } else {
      html.classList.add('portrait');
      html.classList.remove('landscape');
    }
  });
  
  // 设置安全区域
  setupSafeAreaBottom();
  
  // 添加设备类型检测
  if (isIOS()) {
    html.classList.add('ios');
  } else if (isAndroid()) {
    html.classList.add('android');
  }
  
  console.log('Mobile adapter initialized:', {
    deviceType,
    isMobile: isMobile(),
    screenWidth: getScreenWidth(),
    screenHeight: getScreenHeight()
  });
};

// 自动初始化
initMobileAdapter();

export default {
  isMobile,
  isIOS,
  isAndroid,
  getScreenWidth,
  getScreenHeight,
  getDeviceType,
  listenOrientationChange,
  disableZoom,
  setupSafeAreaBottom
}; 